package php4dvdPages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import php4dvdTests.MyPageFactory;

/**
 * @author Anna Alexeyenko
 *         Date: 1/26/13
 */
public class LoginPage extends AnyPage {


    public static final String USER = "admin";
    public static final String PASSWORD = "admin";

    @FindBy(id = "loginform")
    private WebElement loginForm;

    @FindBy(id = "username")
    private WebElement userNameField;

    @FindBy(name = "password")
    private WebElement passwordField;

    @FindBy(name = "submit")
    private WebElement loginButton;

    @Override
    public boolean isOnThisPage() {
        return loginForm.isDisplayed();
    }

    @Override
    public void tryToOpen() {
        MyPageFactory.getPage(driver, InternalPage.class).logout();
    }

    public InternalPage loginWithValidCredentials(String user, String password) {
        userNameField.sendKeys(user);
        passwordField.sendKeys(password);
        loginButton.click();
        return MyPageFactory.getPage(driver, InternalPage.class);

    }

    public LoginPage loginWithInvalidCredentials(String user, String password) {
        userNameField.sendKeys(user);
        passwordField.sendKeys(password);
        loginButton.click();
        return MyPageFactory.getPage(driver, LoginPage.class);

    }

    public InternalPage loginByDefault() {
        return loginWithValidCredentials(USER, PASSWORD);
    }


}
